package nsu.edu.zsq.controller.sys;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;

import nsu.edu.zsq.bean.SchClass;
import nsu.edu.zsq.bean.SchMajor;
import nsu.edu.zsq.bean.Status;
import nsu.edu.zsq.bean.Student;
import nsu.edu.zsq.common.JsonData;
import nsu.edu.zsq.param.PageQuery;
import nsu.edu.zsq.service.sys.CourseService;
import nsu.edu.zsq.service.sys.SchClassService;
import nsu.edu.zsq.service.sys.SchMajorService;

/** 
* Description: 教学班级的controller类<br>
*/
@RequestMapping("/sys/class/teach")
@Controller
public class TeachClassController {
    
    @Autowired
    private SchClassService schClassService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private SchMajorService schMajorService;
    
    /**
     * 获取院系+专业的tree结构数据
     * @return JsonData
     */
    @RequestMapping("/schOrgWithMajorTree")
    @ResponseBody
    public JsonData schOrgWithMajorTree() {
        String listSchOrgWithMajorTree = courseService.listSchOrgWithMajorTree();
        List<SchMajor> listSchMajor = schMajorService.listSchMajor();
        Map<String, Object> map = new HashMap<String, Object>(2);
        map.put("listSchOrgWithMajorTree", listSchOrgWithMajorTree);
        map.put("listSchMajor", listSchMajor);
        return JsonData.success(map);
    }
    
    /**
     * 根据专业代码获取教学班级的分页数据
     * @param majorNo 专业代码
     * @param courseType 课程类型（2：专业选修，）
     * @param pageQuery 分页数据
     * @return JsonData
     */
    @RequestMapping("/listSchClass")
    @ResponseBody
    public JsonData listSchClassWithPage(@RequestParam("majorNo") String majorNo,
            @RequestParam("courseType")  int courseType, PageQuery pageQuery) {
        PageInfo<SchClass> listSch = schClassService.listTeachClassWithPage(majorNo, courseType, pageQuery);
        return JsonData.success(listSch);
    }
    
    /**
     * 获取当前选课的开启状态 state 0：不开启 1：开启
     * @return JsonData
     */
    @RequestMapping("/chooseCourseState")
    @ResponseBody
    public JsonData chooseCourseState() {
        int state = schClassService.chooseCourseState();
        return JsonData.success(state);
    }
    
    /**
     * 更新系统选课的状态信息
     * @param status 系统状态信息
     * @return JsonData
     */
    @RequestMapping("/updateStatus")
    @ResponseBody
    public JsonData updateStatus(Status status) {
        schClassService.updateStatus(status);
        return JsonData.success();
    }
    
    /**
     * 跳转到班级管理页面
     * @param tchCourseId 教师课程id
     * @param model
     * @return String manager/class/teach_class_student.html
     */
    @RequestMapping("/toTeachClass_student")
    public String toTeachClass_student(@RequestParam("tchCourseId")int tchCourseId, Model model) {
        model.addAttribute("tchCourseId", tchCourseId);
        return "manager/class/teach_class_student.html";
    }
    
    /**
     * 获取该选修课程下的所有学生信息
     * @param tchCourseId 教师课程id
     * @param pageQuery 分页信息
     * @return JsonData
     */
    @RequestMapping("/listTeachClassStudent")
    @ResponseBody
    public JsonData listTeachClassStudent(@RequestParam("tchCourseId")int tchCourseId, PageQuery pageQuery) {
        PageInfo<Student> listStudent = schClassService.listTeachClassStudent(tchCourseId, pageQuery);
        return JsonData.success(listStudent);
    }
    
    /**
     * 单个/批量删除学生选课信息
     * @param stuCourseIds 学生课程id数组
     * @return JsonData
     */
    @RequestMapping("/delStuCourse")
    @ResponseBody
    public JsonData delStuCourse(@RequestParam("stuCourseIds[]")int[] stuCourseIds) {
        schClassService.delStuCourse(stuCourseIds);
        return JsonData.success();
    }
}
